using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana.Common
{
    /// <summary>
    /// ActiveEvent用的事件枚举
    /// </summary>
    public enum GameEventKey
    {
        
    }
    
    public class GameEvent : Singleton<GameEvent>
    {
        // ------
        // 这里定义具体的事件
        
        // ------
        
        // ------
        // ActiveEvent
        private Dictionary<GameEventKey, ActiveEvent> _activeEvent = new Dictionary<GameEventKey, ActiveEvent>();

        public void AddActiveEvent(GameEventKey eventKey, ActiveEvent.EventFunc func)
        {
            if (_activeEvent.ContainsKey(eventKey))
            {
                _activeEvent[eventKey].AddListener(func);
            }
            else
            {
                _activeEvent.Add(eventKey, new ActiveEvent(func));
            }
        }

        public void RemoveActiveEvent(GameEventKey eventKey, ActiveEvent.EventFunc func)
        {
            if (_activeEvent.ContainsKey(eventKey))
            {
                _activeEvent[eventKey].RemoveListener(func);
                if (!_activeEvent[eventKey].HasEvent())
                {
                    _activeEvent.Remove(eventKey);
                }
            }
        }

        public void SendActive(ActiveEventArgs args)
        {
            if (_activeEvent.ContainsKey(args.key))
            {
                _activeEvent[args.key].Send(args);
            }
        }
    }

    public class ActiveEventArgs
    {
        public GameEventKey key;
        public object arg;
        public int intParam1;
        public int intParam2;
        public int intParam3;
        public float floatParam1;
        public float floatParam2;
        public float floatParam3;
        public string strParam1;
        public string strParam2;
        public string strParam3;

        public ActiveEventArgs(GameEventKey key)
        {
            this.key = key;
        }
    }
    
    public class ActiveEvent
    {
        public delegate void EventFunc(ActiveEventArgs arg);
        private EventFunc _funcGroup;

        public ActiveEvent(EventFunc func)
        {
            _funcGroup += func;
        }
        
        public void Send(ActiveEventArgs arg)
        {
            _funcGroup?.Invoke(arg);
        }
        
        public void AddListener(EventFunc func)
        {
            _funcGroup += func;
        }

        public void RemoveListener(EventFunc func)
        {
            _funcGroup -= func;
        }

        public bool HasEvent()
        {
            return _funcGroup != null;
        }
    }
    
    public class NIEvent
    {
        public delegate void EventFunc();
        private EventFunc _funcGroup;
        
        public void Send()
        {
            _funcGroup?.Invoke();
        }

        public void AddListener(EventFunc func)
        {
            _funcGroup += func;
        }

        public void RemoveListener(EventFunc func)
        {
            _funcGroup -= func;
        }
    }
    
    public class NIEvent<T>
    {
        public delegate void EventFunc(T arg);
        private EventFunc _funcGroup;
        
        public void Send(T arg)
        {
            _funcGroup?.Invoke(arg);
        }

        public void AddListener(EventFunc func)
        {
            _funcGroup += func;
        }

        public void RemoveListener(EventFunc func)
        {
            _funcGroup -= func;
        }
    }
    
    public class NIEvent<T1, T2>
    {
        public delegate void EventFunc(T1 arg1, T2 arg2);
        private EventFunc _funcGroup;
        
        public void Send(T1 arg1, T2 arg2)
        {
            _funcGroup?.Invoke(arg1, arg2);
        }

        public void AddListener(EventFunc func)
        {
            _funcGroup += func;
        }

        public void RemoveListener(EventFunc func)
        {
            _funcGroup -= func;
        }
    }
    
    public class NIEvent<T1, T2, T3>
    {
        public delegate void EventFunc(T1 arg1, T2 arg2, T3 arg3);
        private EventFunc _funcGroup;
        
        public void Send(T1 arg1, T2 arg2, T3 arg3)
        {
            _funcGroup?.Invoke(arg1, arg2, arg3);
        }

        public void AddListener(EventFunc func)
        {
            _funcGroup += func;
        }

        public void RemoveListener(EventFunc func)
        {
            _funcGroup -= func;
        }
    }
    
    public class NIEvent<T1, T2, T3, T4>
    {
        public delegate void EventFunc(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
        private EventFunc _funcGroup;
        
        public void Send(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
        {
            _funcGroup?.Invoke(arg1, arg2, arg3, arg4);
        }

        public void AddListener(EventFunc func)
        {
            _funcGroup += func;
        }

        public void RemoveListener(EventFunc func)
        {
            _funcGroup -= func;
        }
    }
}
